// import Vue from 'vue';
import { createRouter, createWebHashHistory } from 'vue-router'

/* Layout */
import Layout from '@/layout'

/*
* hidden: true                   侧边栏显示开关 默认false if set true, item will not show in the sidebar(default is false)
* alwaysShow: true               菜单栏显示开关 if set true, will always show the root menu
* children route,                嵌套子集路由 没有不会显示
* meta {
    roles: ['admin','editor']    用户权限 control the page roles (you can set multiple roles)
    title: 'title'               页面标题 the name show in sidebar and breadcrumb (recommend set)
    icon: 'svg-name'/'el-icon-x' 侧边栏图标 the icon show in the sidebar
    noCache: true                缓从开关 默认false  if set true, the page will no be cached(default is false)
    affix: true                  标签固定开关 标签将粘贴在“标签”视图中 if set true, the tag will affix in the tags-view
    breadcrumb: false            面包屑开关 if set false, the item will hidden in breadcrumb(default is true)
    activeMenu: '/example/list'  活动菜单 自定义二级菜单  if set path, the sidebar will highlight the path you set
* }
*
* */

/**
 * constantRoutes
 * a base page that does not have permission requirements 没有权限要求的页面
 * all roles can be accessed 所有角色可以访问的页面
 * 不需要权限  icon: 'dashboard',  icon: 'el-icon-s-help',
 */
export const constantRoutes = [
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [
      {
        path: 'dashboard',
        component: () => import('@/views/dashboard/index'),
        name: 'Dashboard',
        meta: { title: 'Dashboard', affix: true, icon: 'dashboard' },
      },
    ],
  },
  {
    path: '/redirect',
    component: Layout,
    hidden: true,
    children: [
      {
        path: '/redirect/:path(.*)',
        component: () => import('@/views/redirect/index'),
      },
    ],
  },

  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true,
  },
  {
    path: '/documentation',
    component: Layout,
    children: [
      {
        path: 'index',
        component: () => import('@/views/documentation/index'),
        name: 'Documentation',
        meta: { title: 'Documentation', icon: 'documentation', affix: true },
      },
    ],
  },
  {
    path: '/profile',
    component: Layout,
    redirect: '/profile/index',
    hidden: true,
    children: [
      {
        path: 'index',
        component: () => import('@/views/profile/index'),
        name: 'Profile',
        meta: { title: 'Profile', icon: 'user', noCache: true },
      },
    ],
  },

  {
    path: '/mocktest',
    component: Layout,
    children: [
      {
        path: 'index',
        component: () => import('@/views/mocktest/index'),
        name: 'Mocktest',
        meta: { title: 'Mocktest', icon: 'bug' },
      },
    ],
  },
]

/**
 * asyncRoutes
 * the routes that need to be dynamically loaded based on user roles
 * 需要根据用户角色动态加载的路由
 * 有权限才能访问的
 */

export const asyncRoutes = [
  {
    path: '/icons',
    component: Layout,
    children: [
      {
        path: 'index',
        component: () => import('@/views/icons/index'),
        name: 'Icons',
        meta: { title: 'Icons', icon: 'icon', noCache: true },
      },
    ],
  },
  {
    path: '/example',
    component: Layout,
    redirect: '/example/list',
    name: 'Example',
    meta: {
      title: 'Example',
      icon: 'el-icon-s-help',
    },
    children: [
      {
        path: 'create',
        component: () => import('@/views/example/create'),
        name: 'CreateArticle',
        meta: { title: 'Create Article', icon: 'edit' },
      },
      {
        path: 'edit',
        component: () => import('@/views/example/edit'),
        name: 'EditArticle',
        meta: {
          title: 'Edit Article',
          noCache: true,
          activeMenu: '/example/list',
        },
        hidden: true,
      },
      {
        path: 'list',
        component: () => import('@/views/example/list'),
        name: 'ArticleList',
        meta: { title: 'Article List', icon: 'list' },
      },
    ],
  },

  {
    path: '/tab',
    component: Layout,
    children: [
      {
        path: 'index',
        component: () => import('@/views/tab/index'),
        name: 'Tab',
        meta: { title: 'Tab', icon: 'tab' },
      },
    ],
  },

  {
    path: '/external-link',
    component: Layout,
    children: [
      {
        path: 'https://www.baidu.com/',
        meta: { title: 'External Link', icon: 'link' },
      },
    ],
  },
]

// 旧版路由配置
// const createRouter = () => new Router({
//   // mode: 'history', // require service support
//   scrollBehavior: () => ({ y: 0 }),
//   routes: constantRoutes
// })

// 新版router设置
const router = createRouter({
  history: createWebHashHistory(), // 需要从vue-router上引入
  // hash: createWebHashHistory(),
  // abstract: createMemoryHistory(),
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes,
})

// 重启路由
// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  // const newRouter = createRouter()
  // router.resolve = newRouter.matcher // reset router
}

export default router
